package jfox.platform.organization.action;

import java.util.List;
import javax.ejb.EJB;

import jfox.platform.infrastructure.SuperAction;
import jfox.platform.organization.bo.CompanyBO;
import jfox.platform.organization.bo.DepartmentBO;
import jfox.platform.organization.entity.Company;
import jfox.platform.organization.entity.Department;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;
/**
 * @author <a href="mailto:sheshengdian@gmail.com">sheshengdian</a>
 */
@Service(id = "department")
public class DepartmentAction extends SuperAction  {
	   @EJB
	   DepartmentBO departmentBO;
	   @EJB
	   CompanyBO companyBO;
	   /**
	    * 查看所有信息
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "index", successView = "platform/organization/department_list.vhtml")
	    public void listdepartment(ActionContext actionContext) throws Exception {
	        List<Department> departments = departmentBO.getAllDepartments();
	        PageContext pageContext = actionContext.getPageContext();
	        pageContext.setAttribute("departments", departments);
	    }
	   /**
	    * 新增信息
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "newview", successView = "platform/organization/new_department.vhtml")
	    public void newDepartmentView(ActionContext actionContext) throws Exception {

	    }
	   /**
	    * 编辑信息
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "editview", successView = "platform/organization/edit_department.vhtml", invocationClass = DeleteDepartmentInvocation.class)
	    public void editDepartmentView(ActionContext actionContext) throws Exception {
		    DeleteDepartmentInvocation invocation = (DeleteDepartmentInvocation)actionContext.getInvocation();
	        Department department = departmentBO.getDepartmentByCompany(invocation.getId());
	    
	        actionContext.getPageContext().setAttribute("department",department);
	    }
	   /**
	    * 新增保存
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "new", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml",invocationClass = NewDepartmentInvocation.class)
	    public synchronized void newDepartment(ActionContext actionContext) throws Exception {
		   NewDepartmentInvocation invocation = (NewDepartmentInvocation)actionContext.getInvocation();
		    //先增加company表
		   
		    Company company = new Company();
		    company.setName(invocation.getName());
	        company.setParentId(invocation.getParentId());
	        company.setVstatus(invocation.getVstatus());
	        company.setPriority(invocation.getPriority());
	        System.out.print("begin---------");
	        System.out.print(company.getName());
	        System.out.print(company.getParentId());
	        System.out.print("center---------");
	        System.out.print(invocation.getParentId());
	        System.out.print("end---------");
	        companyBO.newCompanyByDepartment(company);
		    //再增加department表
		    Department department = new Department();
	        department.setName(invocation.getName());
	        department.setPriority(invocation.getPriority());
	        department.setVstatus(invocation.getVstatus());
	        department.setManager(invocation.getManager());
	        department.setManagerEmail(invocation.getManagerEmail());
	        department.setManagerPhone(invocation.getManagerPhone());
	        department.setCompanyId(company.getId());
	        departmentBO.newDepartment(department);

	        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, department);
	    }
	   /**
	    * 编辑保存
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "edit", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml",invocationClass = EditDepartmentInvocation.class)
	    public synchronized void editDepartment(ActionContext actionContext) throws Exception {
		   EditDepartmentInvocation invocation = (EditDepartmentInvocation)actionContext.getInvocation();
		   //编辑保存company 
		   Company company = companyBO.getCompanyById(invocation.getCompanyId());
	        company.setName(invocation.getName());
	        company.setVstatus(invocation.getVstatus());
	        company.setPriority(invocation.getPriority());
	        companyBO.updateCompanyByDepartment(company);
		   
		   
	      //编辑保存department 
		   Department department = departmentBO.getDepartmentById(invocation.getId());
	        department.setName(invocation.getName());
	        department.setPriority(invocation.getPriority());
	        department.setVstatus(invocation.getVstatus());
	        department.setManager(invocation.getManager());
	        department.setManagerEmail(invocation.getManagerEmail());
	        department.setManagerPhone(invocation.getManagerPhone());
	        departmentBO.updateDepartment(department);
	        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, department);
	    }
	   /**
	    * 删除信息
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "delete", successView = "include/json_response.vhtml", invocationClass = DeleteDepartmentInvocation.class)
	    public synchronized void deleteModule(ActionContext actionContext) throws Exception {
		    DeleteDepartmentInvocation invocation = (DeleteDepartmentInvocation)actionContext.getInvocation();
	        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, invocation.getCompanyId());
	        departmentBO.deleteDepartment(invocation.getCompanyId());
        	companyBO.deleteCompany(invocation.getCompanyId());
		   /** 
		   try {
	        	DeleteDepartmentInvocation invocation = (DeleteDepartmentInvocation)invocationContext.getInvocation();
	        	departmentBO.deleteDepartment(invocation.getCompanyId());
	        	companyBO.deleteCompany(invocation.getCompanyId());
	        	JSONResponse jsonResponse = JSONResponse.createSuccessJSONResponse(null);
	            invocationContext.getPageContext().setAttribute(JSON_RESPONSE, jsonResponse);
	        }
	        catch (Exception e) {
	        	JSONResponse jsonResponse = JSONResponse.createFailureJSONResponse(null, e);
	            invocationContext.getPageContext().setAttribute(JSON_RESPONSE, jsonResponse);
	            e.printStackTrace();
	        }
	        */
	    }
	   public static class NewDepartmentInvocation extends Invocation {
		    private String name;
		    private String manager;
            private String managerPhone;
            private String managerEmail;
            private long companyId;
            private long parentId;
            
            private int priority;
            private int vstatus;
            
        	public String getName() {
				return name;
			}
            public void setName(String name) {
				this.name = name;
			}
			public String getManager() {
				return manager;
			}

			public void setManager(String manager) {
				this.manager = manager;
			}

			public String getManagerPhone() {
				return managerPhone;
			}

			public void setManagerPhone(String managerPhone) {
				this.managerPhone = managerPhone;
			}

			public String getManagerEmail() {
				return managerEmail;
			}

			public void setManagerEmail(String managerEmail) {
				this.managerEmail = managerEmail;
			}

			public long getCompanyId() {
				return companyId;
			}

			public void setCompanyId(long companyId) {
				this.companyId = companyId;
			}
			public long getParentId() {
				return parentId;
			}

			public void setParentId(long parentId) {
				this.parentId = parentId;
			}
			
			public int getPriority() {
		            return priority;
		        }

		    public void setPriority(int priority) {
		            this.priority = priority;
		        }

		    public int getVstatus() {
		            return vstatus;
		        }

		    public void setVstatus(int vstatus) {
		            this.vstatus = vstatus;
		        }
	   }
	   public static class DeleteDepartmentInvocation extends Invocation {

	        private long id;
	        private long companyId;

	        public long getId() {
	            return id;
	        }

	        public void setId(long id) {
	            this.id = id;
	        }
	        public long getCompanyId() {
	            return companyId;
	        }

	        public void setCompanyId(long companyId) {
	            this.companyId = companyId;
	        }
	    }
	   public static class EditDepartmentInvocation extends NewDepartmentInvocation {

	        private long id;
	        private long companyId;

	        public long getId() {
	            return id;
	        }

	        public void setId(long id) {
	            this.id = id;
	        }
	        public long getCompanyId() {
	            return companyId;
	        }

	        public void setCompanyId(long companyId) {
	            this.companyId = companyId;
	        }
	    }
}
